#include <iostream>
#include <SFML/Graphics.hpp>
#include "../../include/GUI/Button.hpp"

using namespace std;

namespace GUI
{
    Button::Button(Engine &Moteur, const sf::String &text) : Widget(Moteur)
    {
        m_text.SetString(text);
        m_text.SetCharacterSize(14);
        m_text.SetColor(m_Moteur.Config()->GetGUI()->color_contenu);

        m_image.SetImage(*m_Moteur.Graphic()->GetGUIImage(2));

        m_dimensions.width = m_image.GetSize().x-2*m_padding;
        m_dimensions.height = m_image.GetSize().y/3;

        m_rects.push_back(sf::IntRect(0,0,m_image.GetSize().x, m_image.GetSize().y/3));
        m_rects.push_back(sf::IntRect(0,m_image.GetSize().y/3,m_image.GetSize().x, m_image.GetSize().y/3));
        m_rects.push_back(sf::IntRect(0,(2*m_image.GetSize().y)/3,m_image.GetSize().x, m_image.GetSize().y/3));

        m_image.SetSubRect(m_rects[0]); // Le bouton n'est pas survolé ni appuyer

        // Sons

        m_buffer.LoadFromFile("Musics/Sounds/Bouton.wav");
        m_son.SetBuffer(m_buffer);

    }

    bool Button::IsHover()
    {
        int x = m_Moteur.App().GetInput().GetMouseX();
        int y = m_Moteur.App().GetInput().GetMouseY();

        if(x >= m_position.x and y >= m_position.y and x <= m_position.x+m_dimensions.width+2*m_padding and y <= m_position.y+m_dimensions.height)
            return true;
        else
            return false;
    }

    void Button::SetRect(const std::vector<sf::IntRect> &rects)
    {
        m_dimensions.width = m_rects[0].Width-2*m_padding;
        m_dimensions.height = m_rects[0].Height;
        m_rects = rects;
    }
    void Button::SetImage(const unsigned int id)
    {
        m_image.SetImage(*m_Moteur.Graphic()->GetGUIImage(id));
    }
    void Button::SetText(const sf::String &text)
    {
        m_text.SetString(text);
    }

    void Button::Afficher()
    {
        m_image.SetPosition(m_position.x, m_position.y);
        m_text.SetPosition((int)(m_position.x+(m_dimensions.width+2*m_padding-m_text.GetRect().Width)/2), (int)(m_position.y+(m_dimensions.height-m_text.GetRect().Height)/2));
        m_Moteur.Graphic()->Draw(m_image);
        m_Moteur.Graphic()->Draw(m_text);
    }
    void Button::UpdateEvent()
    {
        if(IsClicked())
        {
            if(!m_clicked)
                m_son.Play();
            m_image.SetSubRect(m_rects[2]);
        }
        else if(IsHover())
            m_image.SetSubRect(m_rects[1]);
        else
            m_image.SetSubRect(m_rects[0]);
        m_clicked = IsClicked();
    }

    /*----------------------------------------------------------*/
    /*---------------------BoutonOuverture----------------------*/
    /*----------------------------------------------------------*/

    OpenButton::OpenButton(Engine &Moteur) : Button(Moteur)
    {
        m_image.SetImage(*m_Moteur.Graphic()->GetGUIImage(1));

        m_dimensions.width = 12-2*m_padding;
        m_dimensions.height = 12;

        m_rects.clear();

        m_rects.push_back(sf::IntRect(90,0,12,12));
        m_rects.push_back(sf::IntRect(90,12,12,12));
        m_rects.push_back(sf::IntRect(90,24,12,12));

        m_image.SetSubRect(m_rects[0]); // Le bouton n'est pas survolé ni appuyer
    }
    /*----------------------------------------------------------*/
    /*---------------------BoutonFermeture----------------------*/
    /*----------------------------------------------------------*/

    CloseButton::CloseButton(Engine &Moteur) : Button(Moteur)
    {
        m_image.SetImage(*m_Moteur.Graphic()->GetGUIImage(1));

        m_dimensions.width = 12-2*m_padding;
        m_dimensions.height = 12;

        m_rects.clear();

        m_rects.push_back(sf::IntRect(102,0,12,12));
        m_rects.push_back(sf::IntRect(102,12,12,12));
        m_rects.push_back(sf::IntRect(102,24,12,12));

        m_image.SetSubRect(m_rects[0]); // Le bouton n'est pas survolé ni appuyer
    }

    //------------------------------------------------------
    //--------------------- Onglet -------------------------
    //------------------------------------------------------

    Onglet::Onglet(Engine &Moteur, const sf::String &text) : Button(Moteur, text)
    {
        m_image.SetImage(*m_Moteur.Graphic()->GetGUIImage(5));

        m_dimensions.width = 81-2*m_padding;
        m_dimensions.height = 28;

        m_rects.clear();

        m_rects.push_back(sf::IntRect(81,0,81,28));
        m_rects.push_back(sf::IntRect(0,0,81,28));
        m_rects.push_back(sf::IntRect(0,0,81,28));

        m_image.SetSubRect(m_rects[0]); // Le bouton n'est pas survolé ni appuyer

        m_focus = false; // L'onglet n'a pas le focus par defaut
    }
    void Onglet::SetFocus(bool focus)
    {
        m_focus = focus;

        m_rects.clear();
        if(m_focus)
        {
            m_rects.push_back(sf::IntRect(0,0,81,28));
            m_rects.push_back(sf::IntRect(0,0,81,28));
            m_rects.push_back(sf::IntRect(0,0,81,28));
        }
        else
        {
            m_rects.push_back(sf::IntRect(81,0,81,28));
            m_rects.push_back(sf::IntRect(0,0,81,28));
            m_rects.push_back(sf::IntRect(0,0,81,28));
        }
    }

    bool Onglet::IsFocused()
    {
        return m_focus;
    }
}
